package com.snail.readyvr.util;

import java.security.MessageDigest;

/**
 * Created by MRKING on 2016/11/4.
 */

public class MD5Utils {
    private MD5Utils(){}

    public static String getMD5_16(String sourceStr){

        return encrypt(sourceStr,true);
    }

    public static String getMD5_32(String sourceStr){

        return encrypt(sourceStr,false);
    }
    private static String encrypt(String sourceStr,boolean isSixteen){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(sourceStr.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            if(isSixteen)
            return buf.toString().substring(8, 24).toLowerCase();// 16位的加密
            else
            return buf.toString().toLowerCase();// 32位的加密


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
